package com.quad.innovators.salesease.controller.base;


import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.model.common.Option;
import com.quad.innovators.salesease.model.form.base.ProductForm;
import com.quad.innovators.salesease.model.query.base.ProductPageQuery;
import com.quad.innovators.salesease.model.vo.base.ProductVO;
import com.quad.innovators.salesease.service.base.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "06.产品接口")
@RestController
@RequestMapping(SystemConstants.BACKEND_API + "/product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @Log(value = "分页列表", module = LogModuleEnum.PRODUCT)
    @DataPermission(permissionCode = "base:product:data-permission", userAlias = "t1")
    public Result<PageResultBean<ProductVO>> listPaged(ProductPageQuery queryParams) {
        PageResultBean<ProductVO> result = productService.listPaged(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "新增")
    @PostMapping
    @FunctionPermission(permissionCode = "base:product:add")
    @RepeatSubmit
    public Result<?> save(@RequestBody @Valid ProductForm form) {
        boolean result = productService.save(form);
        return Result.success(result);
    }

    @Operation(summary = "表单数据")
    @GetMapping("/{code}/form")
    @Parameters({
            @Parameter(name = "code", description = "code"),
    })
    @FunctionPermission(permissionCode = "base:product:detail")
    @DataPermission(permissionCode = "base:product:data-permission")
    public Result<ProductForm> getForm(@PathVariable("code") String code) {
        ProductForm formData = productService.getFormData(code);
        return Result.success(formData);
    }

    @Operation(summary = "修改")
    @PutMapping(value = "/{code}")
    @FunctionPermission(permissionCode = "base:product:edit")
    @Parameters({
            @Parameter(name = "code", description = "code")
    })
    @DataPermission(permissionCode = "base:product:data-permission")
    public Result<?> update(@PathVariable("code") String code, @RequestBody @Validated ProductForm form) {
        boolean result = productService.update(code, form);
        return Result.success(result);
    }

    @Operation(summary = "修改状态")
    @PutMapping(value = "/{code}/status")
    @Parameters({
            @Parameter(name = "code", description = "用户ID"),
            @Parameter(name = "status", description = "用户状态(1:启用;0:禁用)")
    })
    @FunctionPermission(permissionCode = "base:product:status")
    @DataPermission(permissionCode = "base:product:data-permission")
    public Result<Void> updateUserStatus(@PathVariable("code") String code, @RequestParam("status") Integer status) {
        boolean result = productService.updateStatus(code, status);
        return Result.judge(result);
    }

    @Operation(summary = "产品列表")
    @GetMapping("/options")
    @DataPermission(permissionCode = "base:product:data-permission")
    public Result<List<Option<String>>> getOptionList() {
        List<Option<String>> menus = productService.listOptions();
        return Result.success(menus);
    }
}
